package com.winning.pmph.config;

import com.baomidou.mybatisplus.core.incrementer.DefaultIdentifierGenerator;
import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator;
import com.baomidou.mybatisplus.core.toolkit.Sequence;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.winning.pmph.prop.MpSequenceProp;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.RequestContextListener;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;

@Configuration
@Import({PaginationInterceptor.class,
        RequestContextListener.class})//监听器：监听HTTP请求事件, 解决RequestContextHolder.getRequestAttributes()空指针问题
public class KmsBeanFactory {
    @Bean
    public Gson gson(){
        return new GsonBuilder().disableHtmlEscaping().create();
    }
    @Bean
    public BufferedImage waterMark() throws IOException {
        return ImageIO.read(Thread.currentThread().getContextClassLoader().getResourceAsStream("public/watermark.png"));
    }

    @Bean
    public DefaultIdentifierGenerator defaultIdentifierGenerator(MpSequenceProp mpSequenceProp) {
        return new DefaultIdentifierGenerator(mpSequenceProp.getWorkerId(), mpSequenceProp.getDatacenterId());
    }

    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}
